package net.adst;

import net.adst.beans.User;
import net.adst.resources.Constants;
import net.adst.resources.VariableNames;
import net.adst.searcher.Searcher;
import net.adst.ui.mainframe.MainFrame;
import net.adst.util.DataManager;
import net.adst.util.MainDataBuilder;
import net.adst.workers.MainFramePaintWorker;

import javax.swing.*;
import javax.swing.plaf.ColorUIResource;
import java.awt.*;
import java.awt.event.WindowEvent;

/**
 * Created with IntelliJ IDEA.
 * User: alekseipot
 * Date: 01.11.12
 * Time: 4:41
 * To change this template use File | Settings | File Templates.
 */
public class AdstControlSystem {
    private MainFrame frame;
    private static AdstControlSystem INSTANCE;

    private AdstControlSystem() {
    }

    public static AdstControlSystem getINSTANCE() {
        return INSTANCE;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                INSTANCE = new AdstControlSystem();
                try {
                    UIManager.put("Viewport.background", new ColorUIResource(Constants.COLOR_WHITE));
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    INSTANCE.init();
                } catch (Exception e) {
                    JOptionPane.showOptionDialog(null, "Что-то пошло не так... \nСвяжить с администратором для решиния проблеммы.", "Ошибка при старте.", JOptionPane.OK_CANCEL_OPTION, JOptionPane.ERROR_MESSAGE, null, null, null);
                    e.printStackTrace();
                }

            }
        });
    }

    private void init() {
//        Locale.setDefault(Locale.US);
//        LoginDialog ld = new LoginDialog();
//        ld.init();
//        User user = (User) DataManager.getObject(VariableNames.USER);
        User user = new User("a", "a");
        user.setAdmin(true);
        DataManager.setObject(VariableNames.USER, user);
        if (user != null) {
            frame = new net.adst.ui.mainframe.MainFrame();
            MainFramePaintWorker paintWorker = new MainFramePaintWorker(frame);
            paintWorker.execute();
            MainDataBuilder.buildData();
            Searcher.searchNaem();
            Searcher.searchOur();
            frame.setVisible(true);
            if (DataManager.getObject(VariableNames.UI_NAEM_NEED_UPDATE_LABEL) != null && DataManager.getObject(VariableNames.UI_OUR_NEED_UPDATE_LABEL) != null) {
                ((JLabel) DataManager.getObject(VariableNames.UI_NAEM_NEED_UPDATE_LABEL)).setVisible(false);
                ((JLabel) DataManager.getObject(VariableNames.UI_OUR_NEED_UPDATE_LABEL)).setVisible(false);
            }

        }
    }


    private boolean confirmExit(WindowEvent e) {
//        if (Boolean.valueOf(ResourceManager.getSettingString("auto.exit"))) {
//            return true;
//        }
        int result = JOptionPane.showConfirmDialog(e.getWindow(),
                "Вы уверены, что хотите выйти?",
                "Выйти", JOptionPane.YES_NO_OPTION);
        return result == JOptionPane.YES_OPTION;
    }

}
